#if UNITY_WEBGL 
using System.Runtime.InteropServices;
using LitJson;
using WeChatWASM;

/// <summary>
/// 微信文件系统的扩展方法
/// TODO 给Editor下每个DllImport方法添加实现
/// </summary>
[FM_Mono.IgnoreGen]
public static class WxFileSystemExt
{
    private static readonly int[] _intOutArray = new int[16];
#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXStatSync(string dirPath, bool recursive);

    public static WXStatResponse StatSync(this WXFileSystemManager wxFileSystemManager, string dirPath, bool recursive)
    {
        string ret = WXStatSync(dirPath, recursive);
        return JsonMapper.ToObject<WXStatResponse>(ret);
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXRmdirSync(string dirPath, bool recursive);

    public static WXStatResponse RmdirSync(this WXFileSystemManager wxFileSystemManager, string dirPath, bool recursive)
    {
        var str = WXRmdirSync(dirPath, recursive);
        return new WXStatResponse();
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXAllocateFileIntOutBuffer(int[] buffer, int length);

    public static void AllocateFileIntOutBuffer(this WXFileSystemManager wxFileSystemManager)
    {
        WXAllocateFileIntOutBuffer(_intOutArray, _intOutArray.Length);
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXOpenStreamSync(string filePath, string flag);

    public static int OpenStreamSync(this WXFileSystemManager wxFileSystemManager, string filePath, string flag, out int length)
    {
        length = 0;
        string error = WXOpenStreamSync(filePath, flag);
        if (string.IsNullOrEmpty(error))
        {
            var fd = _intOutArray[0];
            length = _intOutArray[1];
            return fd;
        }

        return -1;
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXReadSync(int fd, byte[] buffer, int offset, int length, int position);

    /// <summary>
    /// 流式读取
    /// </summary>
    /// <param name="wxFileSystemManager"></param>
    /// <param name="fd">文件描述符的索引，注意js层将实际fd转成int给C#了</param>
    /// <param name="buffer">缓冲数组</param>
    /// <param name="offset">写入缓冲区的偏移</param>
    /// <param name="length">读取长度</param>
    /// <param name="position">读取的起始位置</param>
    /// <returns></returns>
    public static int ReadSync(this WXFileSystemManager wxFileSystemManager, int fd, byte[] buffer, int offset, int length, int position)
    {
        var error = WXReadSync(fd, buffer, offset, length, position);
        if (string.IsNullOrEmpty(error))
        {
            return _intOutArray[0];
        }

        return 0;
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXWriteSync(int fd, byte[] buffer, int offset, int length, int position);

    public static int WriteSync(this WXFileSystemManager wxFileSystemManager, int fd, byte[] buffer, int offset, int length, int position)
    {
        var error = WXWriteSync(fd, buffer, offset, length, position);
        if (string.IsNullOrEmpty(error))
        {
            return _intOutArray[0];
        }

        return 0;
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXFTruncateSync(int fd, int length);

    public static void FTruncateSync(this WXFileSystemManager wxFileSystemManager, int fd, int length)
    {
        WXFTruncateSync(fd, length);
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXFStatSync(int fd);

    public static WXFStatResponse FStatSync(this WXFileSystemManager wxFileSystemManager, int fd)
    {
        string ret = WXFStatSync(fd);
        return JsonMapper.ToObject<WXFStatResponse>(ret);
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern string WXGetFileSizeSync(string filePath);

    public static int GetFileSizeSync(this WXFileSystemManager wxFileSystemManager, string filePath)
    {
        var error = WXGetFileSizeSync(filePath);
        if (string.IsNullOrEmpty(error))
        {
            return _intOutArray[0];
        }

        return -1;
    }


#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
#endif
    private static extern void WXCloseFileSync(int fd);

    public static void CloseFileSync(this WXFileSystemManager wxFileSystemManager, int fd)
    {
        WXCloseFileSync(fd);
    }
}

[FM_Mono.IgnoreGen]
public static class WXStatInfoExt
{
    private const int __S_IFMT = 0xF000;
    private const int S_IFREG = 0x8000; //一般文件
    private const int S_IFDIR = 0x4000; //目录

    public static bool IsFile(this WXStatInfo wxStatInfo)
    {
        return (wxStatInfo.mode & __S_IFMT) == S_IFREG;
    }

    public static bool IsDirectory(this WXStatInfo wxStatInfo)
    {
        return (wxStatInfo.mode & __S_IFMT) == S_IFDIR;
    }
}

[FM_Mono.IgnoreGen]
public class WXFStatResponse : WXBaseResponse
{
    public WXStatInfo stat;
}

#endif